package com.lee.auth.endpoint.verifier;

import com.lee.common.exception.CustomException;
import com.lee.common.exception.FailureStatus;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.lee.common.exception.FailureStatus.BAD_CREDENTIAL;

/**
 * @author lee HN
 * @date 2021/4/16 10:34
 */
@Component
public class ThirdVerifierHolder {

    private final Map<Integer, ThirdVerifier> thirdVerifyMap;

    private static final CustomException UnsupportedThird = new CustomException(BAD_CREDENTIAL, "Unsupported third platforms");

    public ThirdVerifierHolder(ThirdVerifier[] thirdVerifies) {
        this.thirdVerifyMap = new HashMap<>(thirdVerifies.length);
        for (ThirdVerifier thirdVerifier : thirdVerifies)
            thirdVerifyMap.put(thirdVerifier.getProvider(), thirdVerifier);
    }

    public ThirdVerifier obtainThirdVerify(int provider) {
        ThirdVerifier thirdVerifier = thirdVerifyMap.get(provider);
        if (thirdVerifier == null) throw UnsupportedThird;
        return thirdVerifier;
    }

}
